<template>
	<view class="restaurant-manage-container">
		<view v-if="isEmptyObject(restaurantDishes)" class="restaurant-no-dish-manage">暂无菜品数据</view>
		<scroll-view scroll-y :show-scrollbar="true" class="restaurant-manage-scroll">
			<view class="restaurant-dishes-item" v-for="(item,index) in restaurantDishes" v-if="restaurantDishes">
				<!-- 基础信息展示 -->
				<view class="restaurant-dishes-item-info">
					<image class="restaurant-dishes-item-info-image" :src="item.dishImage" mode="aspectFill"></image>
					<view class="restaurant-dishes-item-info-name-price">
						<view class="restaurant-dishes-item-info-name">菜名：{{item.dishName}}</view>
						<view class="restaurant-dishes-item-info-price">价格：{{item.price}}<uni-icons type="heart-filled" color="#ff4500" size="36rpx" style="margin-left: 5rpx;"/></view>
					</view>
				</view>
				<!-- 功能操作 -->
				<view class="restaurant-dishes-item-operation">
					<view class="restaurant-dishes-item-operation-sale">
						<text style="padding-right: 5rpx;">下架</text>
						<switch v-if="onSale" :checked="item.status === 1" color="#FFCC33" style="transform:scale(0.8)" @change="(e) => changeStatus(e, item)"/>
						<switch v-else :checked="item.status === '1'" color="#FFCC33" style="transform:scale(0.8)" @change="(e) => changeStatus(e, item)"/>
						<text style="padding-left: 5rpx;">上架</text>
					</view>
					<view v-if="onSale" class="restaurant-dishes-item-operation-edit-1" @click="clickEdit(item.id)">修改</view>
					<view v-else class="restaurant-dishes-item-operation-edit-delete">
						<view class="restaurant-dishes-item-operation-edit-2" @click="clickEdit(item.id)">修改</view>
						<view class="restaurant-dishes-item-operation-delete" @click="clickDelete(item.id)">删除</view>
					</view>
				</view>
			</view>
			<view class="padding" style="padding: 10rpx;"></view>
		</scroll-view>
	</view>
</template>

<script setup>
	import { ref } from 'vue';
	import { onBeforeRouteLeave } from 'vue-router';
	const props = defineProps({
		onSale:{
			type: Boolean,
			required: true,
			default: true
		},
		restaurantDishes:{
			type: Array,
			required: true,
			default() {
				return [];
			}
		}
	});
	
	const restaurantManage = defineEmits([
		'changeStatus',
		'clickEdit',
		'clickDelete'
	]);
	
	const changeStatus = (event, item) => {
		let newStatus =  event.detail.value ? '1' : '0'; // 这里可能需要检查 event.detail.value 的值是否能正确获取
		
		item.status = newStatus; // 更新原始对象

		// Emit the changeStatus event with the item and new status
		restaurantManage('changeStatus', newStatus, item.id);
	};
	
	const isEmptyObject = (obj) => {
	    return Object.keys(obj).length === 0;
	};
	
	//点击修改
	const clickEdit = (id) => {
		restaurantManage('clickEdit', id);
	};
	
	//点击删除
	const clickDelete = (id) => {
		restaurantManage('clickDelete', id);
	}
	
	
</script>

<style lang="scss" scoped>

	.restaurant-manage-container{
		height: calc(100vh - 90px);
		overflow-y: hidden;
		.restaurant-manage-scroll{
			height: 100%;
			.restaurant-dishes-item{
				width: 690rpx;
				margin: 50rpx auto;
				border-radius: 10rpx;
				box-shadow: 0 0 30rpx rgba(0, 0, 0, 0.1);
				.restaurant-dishes-item-info{
					display: flex;
					align-items: center;
					height: 200rpx;
					margin: 30rpx;
					border-bottom: 1px solid #a7a7a7;
					.restaurant-dishes-item-info-image{
						width: 160rpx;
						height: 120rpx;
						border-radius: 10rpx;
					}
					.restaurant-dishes-item-info-name-price{
						margin-left: 110rpx;
						font-size: 36rpx;
						.restaurant-dishes-item-info-name{
							padding-bottom: 20rpx;
						}
					}
				}
				.restaurant-dishes-item-operation{
					height: 120rpx;
					display: flex;
					align-items: center;
					padding: 0 30rpx 0 30rpx;
					font-size: 36rpx;
					.restaurant-dishes-item-operation-sale{
						display: flex;
						align-items: center;
						height: 100rpx;
					}
					.restaurant-dishes-item-operation-edit-1{
						width: 120rpx;
						height: 100rpx;
						display: flex;
						align-items: center;
						justify-content: center;
						border-radius: 20rpx;
						background-color: $brand-theme-color;
						margin-left: 180rpx;
					}
					.restaurant-dishes-item-operation-edit-delete{
						display: flex;
						.restaurant-dishes-item-operation-edit-2{
							display: flex;
							align-items: center;
							justify-content: center;
							border-radius: 20rpx;
							width: 120rpx;
							height: 100rpx;
							background-color: $brand-theme-color;
							margin-left: 50rpx;
						}
						.restaurant-dishes-item-operation-delete{
							display: flex;
							align-items: center;
							justify-content: center;
							border-radius: 20rpx;
							width: 120rpx;
							height: 100rpx;
							background-color: #FFCCCB ;
							margin-left: 50rpx;
						}
					}
				}
			}
		}
		.restaurant-no-dish-manage{
			font-size: 50rpx;
			display: flex;
			justify-content: center;
			padding-top: 50rpx;
		}
	}
</style>